﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ReflexTérkép.Tests.dto;

namespace ReflexTérkép.Tests
{
    [TestClass]
    public class CachingTests : TestBase
    {
        [TestMethod]
        public void Previously_Instantiated_Objects_Will_Be_Returned_Until_The_Cache_Is_Cleared()
        {
            // Arrange
            var dictionary = new Dictionary<string, object>
                                 {
                                     { "CustomerId", 1 },
                                     { "FirstName", "Bob" },
                                     { "LastName", "Smith" }
                                 };

            // Act
            var customer = ReflexMapper.Map<Customer>(dictionary);

            // Assert
            Assert.IsTrue(customer.FirstName == "Bob");

            // Arrange
            var dictionary2 = new Dictionary<string, object>
                                  {
                                      { "CustomerId", 1 }
                                  };

            // Act
            var customer2 = ReflexMapper.Map<Customer>(dictionary2);

            // Assert that this will be "Bob" because the identifier of the Customer object was the same, 
            // so we recieved back the cached instance of the Customer object.
            Assert.IsTrue(customer2.FirstName == "Bob");

            // Arrange
            var dictionary3 = new Dictionary<string, object>
                                  {
                                      { "CustomerId", 1 }
                                  };

            ReflexMapper.Cache.ClearInstanceCache();

            // Act
            var customer3 = ReflexMapper.Map<Customer>(dictionary3);

            // Assert
            Assert.IsTrue(customer3.FirstName == null);
        }
    }
}
